// 本文件为title组件相关API

import axios from "@/js/my_axios"


//获取测试详情
function get_test_info(page, page_size) {
    return axios({
        method: 'get',
        url: 'regression/',
        params: {
            'page': page,
            'page_size': page_size,
        },
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function query_filter(params) {
    return axios({
        method: 'get',
        url: 'regression/',
        params: params
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

//创建任务
function add_test(test_info) {
    if (!test_info) {
        return
    }
    return axios({
        method: 'post',
        url: 'regression/',
        data: test_info,
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

//修改任务
function modify_test(test_info) {
    if (!test_info) {
        return
    }
    return axios({
        method: 'put',
        url: 'regression/',
        data: test_info,
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

// 删除
function delete_test_info(val) {
    var status = false
    var val_list = []
    if (Array.isArray(val)) {
        val_list = JSON.parse(JSON.stringify(val))
        status = true
    }
    return axios({
        method: 'delete',
        url: 'regression/',
        data: {
            info: status ? val_list : val,
        },
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

// 过滤查询
function filter_query(page, page_size, filter) {
    return axios({
        method: 'get',
        url: 'regression/',
        params: {
            'page': page,
            'page_size': page_size,
            'time_filter': filter ? filter : '',
        },
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function start_task(val) {
    // 插入测试任务到schedule表
    let data = {
        data: val,
        method: 'start'
    }
    return axios({
        method: 'post',
        url: 'regression_execute/',
        data: data
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function stop_or_continue_task(val) {
    // 停止或继续
    let info = {
        data: val,
    }
    if (val.status === 'stop'){
        info['method'] = 'continue'
    }else{
        info['method'] = 'stop'
    }
    return axios({
        method: 'post',
        url: 'regression_execute/',
        data: info
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function kill_task(val) {
    // 停止或继续
    let info = {
        data: val,
        method: 'kill'
    }
    return axios({
        method: 'post',
        url: 'regression_execute/',
        data: info
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function read_log(val) {
    // 停止或继续
    let info = {
        file_path: val,
    }
    return axios({
        method: 'post',
        url: 'read_log/',
        data: info,
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}

function get_police(val) {
    // 获取报警机器人列表
    return axios({
        method: 'get',
        url: 'police/',
    }).then((response) => {
        return response
    }).catch((error) => {
        console.log(error);
    });
}


export {
    add_test,
    modify_test,
    get_test_info,
    query_filter,
    delete_test_info,
    filter_query,
    start_task,
    stop_or_continue_task,
    kill_task,
    read_log,
    get_police,
}